﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace FrogEngine
{
	public class FireParticleSystem : GPUParticleSystem<SimpleGPUParticleVertex, FireParticleSettings>
	{
		public FireParticleSystem(IEngine engine)
			: base(engine) { }

		public override void Render(GameTime gameTime)
		{
			base.Render(gameTime);
		}

		public override IEnumerable<string> AssetNames()
		{
			return new List<string> { this.settings.TextureAssetName, "ParticleEffect" };
		}

		public override void LoadContent()
		{
			this.tex = ContentManager.Load<Texture2D>(this.settings.TextureAssetName);
			var effect = ContentManager.Load<Effect>("ParticleEffect");
			
			this.shader = effect; // just in case
		}

		private Texture2D tex;

		public override void SetShaderParameters(GraphicsDevice device, ICameraService cameraService, float systemTimer)
		{
			// Set an effect parameter describing the viewport size. This is
			// needed to convert particle sizes into screen space point sizes.
			shader.Parameters["ViewportScale"].SetValue(new Vector2(0.5f / device.Viewport.AspectRatio, -0.5f));

			// Set an effect parameter describing the current time. All the vertex
			// shader particle animation is keyed off this value.
			shader.Parameters["CurrentTime"].SetValue(systemTimer);

			shader.Parameters["View"].SetValue(cameraService.GetActiveCamera().View);
			shader.Parameters["Projection"].SetValue(cameraService.GetActiveCamera().Projection);

			var parameters = this.shader.Parameters;

			parameters["Duration"].SetValue(settings.LifeTime);
			parameters["DurationRandomness"].SetValue(settings.DurationRandomness);
			parameters["Gravity"].SetValue(settings.Gravity);
			parameters["EndVelocity"].SetValue(settings.EndVelocity);
			parameters["MinColor"].SetValue(settings.MinColor.ToVector4());
			parameters["MaxColor"].SetValue(settings.MaxColor.ToVector4());

			parameters["RotateSpeed"].SetValue(
				new Vector2(settings.MinRotateSpeed, settings.MaxRotateSpeed));

			parameters["StartSize"].SetValue(
				new Vector2(settings.MinStartSize, settings.MaxStartSize));

			parameters["EndSize"].SetValue(
				new Vector2(settings.MinEndSize, settings.MaxEndSize));

			parameters["Texture"].SetValue(this.tex);
		}
	}
}
